<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		loginToken,
		userStore
	} from '@/stores'

	const isChecked = ref(false)
	const userInfo = ref({})
	const redirectUrl = ref('')
	const token = ref('4f7a1b9e3c8d0f5a2b6e1d4c9f8a3b73')
	const tokenStore = loginToken()
	const userInfoStore = userStore()
	const tabbarList = [
		'/pages/login/login',
		'/pages/journey/journey',
		'/pages/my/my'
	]
	//返回上一页
	const back = () => {
		goBack()
	}
	const goBack = () => {
		if (!redirectUrl.value) {
			uni.navigateBack({
				delta: 1
			})
			console.log(1)
			return
		}
		if (tabbarList.some(path => redirectUrl.value.includes(path))) {
			uni.switchTab({
				url: '/pages/my/my'
			})
		} else {
			uni.redirectTo({
				url: redirectUrl.value
			})
			console.log(3)
			console.log(redirectUrl.value);
		}
	}
	//更改checked值
	const changChecked = (e) => {
		isChecked.value = e.detail.value
	}
	//处理登录
	const handleGetUserProfile = () => {
		if (isChecked.value.length > 0) {
			uni.showModal({
				title: '登录测试',
				content: '确认登录吗？',
				success(res) {
					if (res.confirm) {
						uni.getUserProfile({
							desc: '用于完善您的个人资料',
							success: (profileRes) => {
								userInfo.value = {
									nickName: profileRes.userInfo.nickName,
									avatarUrl: profileRes.userInfo.avatarUrl
								}
								tokenStore.setToken(token.value)
								uni.setStorageSync('userInfo', userInfo.value)
								uni.showToast({
									title: '登录成功'
								})
								goBack()
							},
							fail: (err) => {
								console.error('获取用户信息失败:', err)
								uni.showToast({
									title: '授权失败',
									icon: 'none'
								})
							}
						})
					}
				}
			})
		} else {
			uni.showToast({
				title: '请勾选协议'
			})
		}
	}

	onLoad((options) => {
		if (options.redirect) {
			redirectUrl.value = decodeURIComponent(options.redirect)
		}
	})
</script>

<template>
	<view class="my">
		<uni-nav-bar dark :fixed="true" background-color="#03bf5f" status-bar left-icon="left" title="登录"
			@clickLeft="back" />
		<view class="logo">
			<image src="/static/images/wel.png"></image>
		</view>
		<view class="detail">
			<text class="welcome">欢迎使用漳州城际出行</text>
			<view class="agreement">
				<checkbox-group class="choose" @change="changChecked">
					<label>
						<checkbox :checked="isChecked" />
						<view class="content">我已阅读并同意<navigator url="/pages/agreement/personal">《用户协议》</navigator>和
							<navigator>
								《个人信息保护规则》</navigator>
						</view>
					</label>
				</checkbox-group>

			</view>
			<button @click="handleGetUserProfile" class="button">微信一键登录</button>
		</view>
	</view>
</template>

<style lang="scss" scoped>
	@import '../../styles/_rpx.scss';

	.choose {
		label {
			display: flex;
		}

		checkbox {
			transform: scale(0.6);
		}
	}

	.logo {
		text-align: center;
	}

	.logo image {
		width: rpx(180);
		height: rpx(180);
	}

	.detail {
		text-align: center;
	}

	.welcome {
		font-size: rpx(17);
		font-weight: bold;
	}

	.agreement {
		width: 100%;
		display: flex;
		justify-content: center;
		margin: rpx(39) 0 rpx(21) 0;
		font-size: rpx(10);

		.checkbox {
			transform: scale(0.5)
		}

		.content {
			display: flex;
			margin: auto 0;
			justify-content: center;

			navigator {
				color: #39cb90;
			}
		}
	}

	.button {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		flex: 1;
		width: rpx(307);
		height: rpx(39);
		margin: auto;
		border-radius: rpx(5);
		background-color: #03bf5f;
	}
</style>